package com.new_wear.project.system.area.controller;

import java.util.List;

import com.new_wear.framework.web.domain.Ztree;
import com.new_wear.project.article.article.domain.CstArticle;
import com.new_wear.project.article.article.service.ICstArticleService;
import com.new_wear.project.system.group.domain.CstVolunteerGroup;
import com.new_wear.project.system.menu.service.MenuServiceImpl;
import com.new_wear.project.system.role.domain.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.area.domain.CstArea;
import com.new_wear.project.system.area.service.ICstAreaService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;

/**
 * 系统 - 地区Controller
 * 
 * @author new_wear
 * @date 2020-01-07
 */
@Controller
@RequestMapping("/system/area")
public class CstAreaController extends BaseController
{
    private String prefix = "system/area";

    @Autowired
    private ICstAreaService cstAreaService;
    @Autowired
    private MenuServiceImpl menuService;

    @RequiresPermissions("system:area:view")
    @GetMapping()
    public String area()
    {
        return prefix + "/area";
    }

    /**
     * 查询系统 - 地区列表
     */
    @RequiresPermissions("system:area:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CstArea> list(CstArea cstArea)
    {
        startPage();
        List<CstArea> list = cstAreaService.selectCstAreaList(cstArea);
        return list;
    }

    /**
     * 导出系统 - 地区列表
     */
    @RequiresPermissions("system:area:export")
    @Log(title = "系统 - 地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstArea cstArea)
    {
        List<CstArea> list = cstAreaService.selectCstAreaList(cstArea);
        ExcelUtil<CstArea> util = new ExcelUtil<CstArea>(CstArea.class);
        return util.exportExcel(list, "area");
    }

    /**
     * 新增系统 - 地区
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统 - 地区
     */
    @RequiresPermissions("system:area:add")
    @Log(title = "系统 - 地区", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstArea cstArea)
    {
        cstArea.setPid(0);
        return toAjax(cstAreaService.insertCstArea(cstArea));
    }

    /**
     * 修改系统 - 地区
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstArea cstArea = cstAreaService.selectCstAreaById(id);
        mmap.put("cstArea", cstArea);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统 - 地区
     */
    @RequiresPermissions("system:area:edit")
    @Log(title = "系统 - 地区", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstArea cstArea)
    {
        return toAjax(cstAreaService.updateCstArea(cstArea));
    }

    /**
     * 删除系统 - 地区
     */
    @RequiresPermissions("system:area:remove")
    @Log(title = "系统 - 地区", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(cstAreaService.deleteCstAreaByIds(id.toString()));
    }



    /*树结构*/
    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Ztree> roleMenuTreeData(Role role)
    {
        List<Ztree> ztrees = menuService.roleMenuTreeData(role);
        return ztrees;
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Ztree> menuTreeData(Role role)
    {
        List<Ztree> ztrees = cstAreaService.menuTreeData();
        return ztrees;
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{id}")
    public String selectMenuTree(@PathVariable("id") Integer id, ModelMap mmap)
    {

        mmap.put("group", cstAreaService.selectCstAreaById(id));
        return prefix + "/tree";
    }


    @GetMapping("/areaAdd/{id}")
    public String areaAdd(@PathVariable("id") Integer id,ModelMap map){
        map.put("id",id);
        return prefix + "/cstAreaAdd";
    }
    @RequestMapping("/newCstAreaAdd")
    @ResponseBody
    public AjaxResult addSublevel(@RequestParam("pid") Integer pid , CstArea cstArea){
        cstArea.setPid(pid);
        return toAjax(cstAreaService.insertCstArea(cstArea));
    }
}
